<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['menutypes']=array(
	'1'=>'默认菜单',
	'2'=>'个性化菜单'
);
$return['statuss']=array(
	'1'=>'已在微信端生效',
	'0'=>'未在微信端生效'
);
$return['menu_status_style']=array(
	'1'=>'label-success',
	'0'=>'label-danger'
);
$return['wechats']=pdo_getall('core_account_wechat',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
if($_RQ['op']== 'list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['wechat_id']){
		$params['wechat_id']=intval($_RQ['wechat_id']);
		$return['wechat_id']=intval($_RQ['wechat_id']);
	}
	$return['list']=pdo_getall('core_account_menus',$params,array('id','type','title','status','createtime'));

	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'菜单列表'),
			array('op'=>'edit','title'=>'添加默认菜单','query'=>'type=1'),
			array('op'=>'edit','title'=>'添加个性化菜单','query'=>'type=2'),
		),
		'list'=>array('data'=>'list','pagelist'=>false,'delall'=>false,'list'=>array(
			array('type'=>'string','name'=>'title','title'=>'标题'),
			array('type'=>'time','name'=>'createtime','title'=>'创建时间'),
			array('type'=>'radio','name'=>'type','title'=>'类型','options'=>'menutypes'),
			array('type'=>'radio','name'=>'status','title'=>'是否生效','options'=>'statuss'),
			
		),'action'=>array(
			array('type'=>'post','op'=>'set','message'=>'确定推送到微信端？','title'=>'推送到微信端'),
			array('type'=>'post','op'=>'cancel','message'=>'确定从微信端删除本菜单？','title'=>'从微信端删除','show'=>array('status'=>'1')),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	exi($return);
}
if($_RQ['op']== 'edit') {
	$return['province']=file_get_contents(ROOT_D.'/core/resource/area.txt');
	$return['province']=unserialize($return['province']);
	$return['languages'] = array(
		array('ch'=>'简体中文', 'en'=>'zh_CN'),
		array('ch'=>'繁体中文TW', 'en'=>'zh_TW'),
		array('ch'=>'繁体中文HK', 'en'=>'zh_HK'),
		array('ch'=>'英文', 'en'=>'en'),
		array('ch'=>'印尼', 'en'=>'id'),
		array('ch'=>'马来', 'en'=>'ms'),
		array('ch'=>'西班牙', 'en'=>'es'),
		array('ch'=>'韩国', 'en'=>'ko'),
		array('ch'=>'意大利 ', 'en'=>'it'),
		array('ch'=>'日本', 'en'=>'ja'),
		array('ch'=>'波兰', 'en'=>'pl'),
		array('ch'=>'葡萄牙', 'en'=>'pt'),
		array('ch'=>'俄国', 'en'=>'ru'),
		array('ch'=>'泰文', 'en'=>'th'),
		array('ch'=>'越南', 'en'=>'vi'),
		array('ch'=>'阿拉伯语', 'en'=>'ar'),
		array('ch'=>'北印度', 'en'=>'hi'),
		array('ch'=>'希伯来', 'en'=>'he'),
		array('ch'=>'土耳其', 'en'=>'tr'),
		array('ch'=>'德语', 'en'=>'de'),
		array('ch'=>'法语', 'en'=>'fr')
	);
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_account_menus',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['button']=json_decode($item['button'],true);
		$item['matchrule']=$item['matchrule']['sex']>=0?json_decode($item['matchrule']):array('sex'=>1);
	}elseif(intval($_RQ['type'])){
		$item=array('type'=>intval($_RQ['type']),'button'=>array());
		if($_RQ['type']==2){
			$check=pdo_getcolumn('core_account_menus',array('uniacid'=>$_SESSION['uniacid'],'type'=>1,'status'=>1),'id');
			if(!$check){
				exi('根据微信官方的要求，创建个性菜单前，请先创建默认菜单！','error');
			}
		}
	}else{
		exi('参数错误！');
	}
	$return['type']=$item['type'];
	$return['item']=$item;
	$return['groups']=pdo_getall('core_fansgroup',array('uniacid'=>$_SESSION['uniacid']));
	exi($return);
}
if($_RQ['op']== 'post') {
	$postdata=get_postdata();
	//校验菜单
	foreach($postdata['button'] as $key=>&$button){
		if(!$button['name']){
			exi('请设置第'.($key+1).'个主菜单名称！','error');
		}
		if($button['sub_button']){
			$button=array_fields(array('name','sub_button'),$button);
			foreach($button['sub_button'] as $sub_key=>&$sub){
				if(!$sub['name']){
					exi('请设置第'.($key+1).'个主菜单的第'.($sub_key+1).'个子菜单名称名称！','error');
				}
				if(!$sub['key']){
					$sub['key']='menu_'.$key.'_'.$sub_key;
				}
			}
		}else{
			if(!$button['key']){
				$button['key']='menu_'.$key;
			}
		}
	}
	
	$data=array(
		'wechat_id'=>intval($postdata['wechat_id']),
		'type'=>intval($postdata['type']),
		'title'=>trim($postdata['title']),
		'matchrule'=>ijson_encode($postdata['matchrule']),
		'button'=>json_encode($postdata['button']),
	);
	if(!$data['wechat_id']){
		exi('请先设置应用于哪个公众号！');
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_account_menus',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('修改菜单成功！如需使其在微信端生效，请在接下来的页面中选择并点击推送到微信端。','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('core_account_menus',$data);
		exi('增加菜单成功！如需使其在微信端生效，请在列表页选择并点击推送到微信端。','','list');
	}
}
if($_RQ['op']== 'del') {
	//删除菜单
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择菜单！','error');
	}
	pdo_delete('core_account_menus',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('删除成功！');
}
if($_RQ['op']== 'cancel') {
	//删除公众平台菜单
	$id=intval($_RQ['id']);
	$menu=pdo_get('core_account_menus',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('wechat_id','type'));
	if(!$menu['type']){
		exi('参数错误！');
	}elseif($menu['type']==1){
		cfc('account',$menu['wechat_id'])->del_account_menu($id);
	}elseif($menu['type']==2){
		cfc('account',$menu['wechat_id'])->del_account_diymenu($id);
	}else{
		exi('错误的菜单类型：'.$menu['type'],'error');
	}
	exi('删除成功！');
}
if($_RQ['op']== 'set'){
	//设置菜单生效
	$id=intval($_RQ['id']);
	$menu=pdo_get('core_account_menus',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('wechat_id','type'));
	if(!$menu['type']){
		exi('参数错误！','error');
	}elseif($menu['type']==1){
		cfc('account',$menu['wechat_id'])->set_account_menu($id);
	}elseif($menu['type']==2){
		cfc('account',$menu['wechat_id'])->set_account_diymenu($id);
	}else{
		exi('错误的菜单类型：'.$menu['type'],'error');
	}
	pdo_update('core_account_menus',array('status'=>1),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('设置成功！新的菜单设置将在十分钟后生效。如需立即体验，可取消关注公众号再重新关注。');
}
if($_RQ['op']== 'wxapp'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=trim($_RQ['title']);
	}
	$fields=array('id','title','appid');
	$pindex = max(1, $_RQ['page']);
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_account_wxapp',$params,array($pindex,$psize),$total,$fields,'','id desc');
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'小程序列表'),
			array('op'=>'add','title'=>'创建小程序'),
		),
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			array('type'=>'string','title'=>'小程序名称','name'=>'title'),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'appid','title'=>'选取'),
		)),
		'search_one'=>array('name'=>'title','add_op'=>'add'),
	);

	$return['htmls']=$htmls;
	exi($return);
}